﻿namespace ChinaUnicom
{
    using Avatar.Themes;
    using System;
    using System.IO.IsolatedStorage;

    public class ApplicationSettings
    {
        const string PrimelyFeeHistoryDateKey = "PrimelyFeeHistoryDateKey";
        const string ThemeSettings = "ThemeSettings";

        static IsolatedStorageSettings storage
        {
            get
            {
                return IsolatedStorageSettings.ApplicationSettings;
            }
        }

        static object locker = new object();

        private ApplicationSettings()
        {
        }

        static ApplicationSettings settings;
        public static ApplicationSettings Settings
        {
            get
            {
                if (settings == null)
                {
                    lock (locker)
                    {
                        settings = new ApplicationSettings();
                    }
                }

                return settings;
            }
        }

        public string this[string key]
        {
            get
            {
                if (!storage.Contains(key))
                {
                    return string.Empty;
                }

                return storage[key].ToString();
            }

            set
            {
                storage[key] = value;
            }
        }

        /// <summary>
        /// 最早的消费记录时间，表明此前没有记录了
        /// </summary>
        public DateTime PrimelyFeeHistoryDate
        {
            get
            {
                if (storage.Contains(PrimelyFeeHistoryDateKey))
                {
                    return DateTime.Parse(this[PrimelyFeeHistoryDateKey]);
                }

                return DateTime.MinValue;
            }
            set
            {
                storage[PrimelyFeeHistoryDateKey] = value;
            }
        }
        
        /// <summary>
        /// 系统主题设置
        /// </summary>
        public Theme Theme
        {
            get
            {
                if (storage.Contains(ThemeSettings))
                {
                    return (Theme)storage[ThemeSettings];
                }

                return Theme.System;
            }
            set
            {
                storage[ThemeSettings] = value;
            }
        }
    }
}
